//
//  YMSampleWaitViewController.m
//  YMMedical
//
//  Created by y s on 2023/7/24.
//

#import "YMSampleWaitViewController.h"
#import "YMHomeTipInforAlert.h"

@interface YMSampleWaitViewController ()

@property (nonatomic, strong) UIScrollView *svMain;
@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UIImageView *imgvTop;

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) NSTimer *outTimer;
@property (nonatomic, strong) UILabel *lblTime;
@property (nonatomic, strong) UILabel *lblTTitle;
//@property (nonatomic, strong) UIButton *btnStop;

@property (nonatomic, strong) UIView *vBox;
@property (nonatomic, strong) UILabel *lblTips;
@property (nonatomic, strong) UIImageView *imgvIcon;
@property (nonatomic, strong) UIButton *btnFinish;

@property (nonatomic, assign) NSInteger timeAll;
//是否超时
@property (nonatomic, assign) BOOL isOut;
//是否可以展示 超时
@property (nonatomic, assign) BOOL canShowOut;

@end

@implementation YMSampleWaitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _canShowOut = YES;
//    [self timeOutShowAlert];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _canShowOut = NO;
}

- (void)setUI{
    [self.view addSubview:self.svMain];
    [self.svMain addSubview:self.vBg];
    [self.vBg addSubview:self.imgvTop];
//    [self.vBg addSubview:self.btnStop];
    
    [self.vBg addSubview:self.vBox];
    [self.vBox addSubview:self.lblTips];
    [self.vBox addSubview:self.imgvIcon];
    
    [self.view addSubview:self.btnFinish];
    
    [self.svMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.width.equalTo(self.svMain);
        make.bottom.mas_equalTo(0);
    }];
    [self.imgvTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fit:200]);
    }];
//    [self.btnStop mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.imgvTop.mas_bottom).offset([SFitUtil fit:20]);
//        make.size.mas_equalTo([SFitUtil fitSize:150 height:48]);
//        make.centerX.mas_equalTo(0);
//    }];
    [self.vBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgvTop.mas_bottom).offset([SFitUtil fit:30]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.lblTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo([SFitUtil fit:15]);
    }];
    [self.imgvIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblTips.mas_bottom).offset([SFitUtil fit:23]);
        make.height.mas_equalTo([SFitUtil fit:98.5]);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo([SFitUtil fit:-63]);
    }];
    [self.btnFinish mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-IPHONE_X_BOTTOM_HEIGHT-[SFitUtil fit:15]);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
        make.centerX.mas_equalTo(0);
        make.top.equalTo(self.svMain.mas_bottom).offset([SFitUtil fit:20]);
    }];
    self.lblTime.text = [self getTimeStringByTime:_waitTime*60];
    [self starTimer];
}



- (void)starTimer{
    [self stopTimer];
    _timeAll = _waitTime*60;
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(startTimerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)stopTimer{
    if (_timer){
        [self.timer invalidate];
        self.timer = nil;
    }
}
- (void)startTimerAction{
    if (_isOut){
        if (self.timeAll == _outTime*60){
//            [self timeOutShowAlert];
        }
        self.timeAll += 1;
        self.lblTime.textColor = [SUIUtil colorOf:0xFF3141];
        self.lblTTitle.textColor = [SUIUtil colorOf:0xFF3141];
        self.lblTTitle.text = KLanguage(@"expired_txt");
    }else{
        self.timeAll -= 1;
        if (self.timeAll == 0){
            _isOut = YES;
            [self shakeToShow];
        }
        self.lblTime.textColor = STheme.D12;
        self.lblTTitle.textColor = STheme.D12;
        self.lblTTitle.text = KLanguage(@"count_down_txt");
    }
    self.lblTime.text = [self getTimeStringByTime:self.timeAll];
}
- (void)timeOutShowAlert{
    if (_isOut && _canShowOut){
        YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
        alert.iconName = @"alert_tips_outtime_icon";
        alert.strContent = KLanguage(@"waiting_time_expired_txt");
        WEAK_SELF;
        alert.sureBlock = ^{
            if (weakSelf.timeOutBlock){
                weakSelf.timeOutBlock();
            }
        };
        [alert show];
    }
}

- (void)finishClickAction:(id)sender{
    if (_isOut){
        if (self.timeAll <= _outTime*60){
            if (self.finishBlock){
                self.finishBlock();
            }
        }else{
            if (self.timeOutBlock){
                self.timeOutBlock();
            }
        }
    }else{
        YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
        alert.iconName = @"alert_tips_outtime_icon";
        alert.strContent = KLanguage(@"time_not_finish_continue_next_txt");
        WEAK_SELF;
        alert.sureBlock = ^{
            if (weakSelf.finishBlock){
                weakSelf.finishBlock();
            }
        };
        [alert show];
    }
    
}

- (NSString *)getTimeStringByTime:(NSInteger)reduceTime{
    if (reduceTime >= 3600){
        NSString *str_hour = [NSString stringWithFormat:@"%02ld",reduceTime/3600];
        NSString *str_minute = [NSString stringWithFormat:@"%02ld",(reduceTime%3600)/60];
        NSString *str_second = [NSString stringWithFormat:@"%02ld",reduceTime%60];
        return [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    }else{
        NSString *str_minute = [NSString stringWithFormat:@"%02ld",(reduceTime%3600)/60];
        NSString *str_second = [NSString stringWithFormat:@"%02ld",reduceTime%60];
        return [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    }
}

- (void)shakeToShow
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 1.0;// 动画时间
    NSMutableArray *values = [NSMutableArray array];
    // 前两个是控制view的大小的；
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.repeatCount = MAXFLOAT;
    [_btnFinish.layer addAnimation:animation forKey:nil];
}

#pragma mark -- setter and getter
- (UIScrollView *)svMain{
    if (_svMain == nil){
        _svMain = [[UIScrollView alloc] init];
        _svMain.showsVerticalScrollIndicator = YES;
        _svMain.showsHorizontalScrollIndicator = NO;
    }
    return _svMain;
}
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
    }
    return _vBg;
}
- (UIImageView *)imgvTop{
    if (_imgvTop == nil){
        _imgvTop = [[UIImageView alloc] init];
        _imgvTop.image = [UIImage imageNamed:@"check_smapling_wait_top_bg"];
        
        UIView *vBig = [[UIView alloc] init];
        vBig.clipsToBounds = YES;
        vBig.layer.cornerRadius = [SFitUtil fit:150]*0.5;
        vBig.layer.borderWidth = [SFitUtil fit:1];
        vBig.layer.borderColor = STheme.D9.CGColor;
        vBig.backgroundColor = [SUIUtil colorOf:0x999999 alpha:0.4];
        
        UIView *vSamll = [[UIView alloc] init];
        vSamll.clipsToBounds = YES;
        vSamll.layer.cornerRadius = [SFitUtil fit:132]*0.5;
        vSamll.layer.borderWidth = [SFitUtil fit:1];
        vSamll.layer.borderColor = STheme.D12.CGColor;
        vSamll.backgroundColor = STheme.D9;
        [_imgvTop addSubview:vBig];
        [vBig addSubview:vSamll];
        [vSamll addSubview:self.lblTime];
        [vSamll addSubview:self.lblTTitle];
        
        [vBig mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo([SFitUtil fit:20]);
            make.centerX.mas_equalTo(0);
            make.size.mas_equalTo([SFitUtil fitSize:150 height:150]);
        }];
        [vSamll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(vBig);
            make.size.mas_equalTo([SFitUtil fitSize:132 height:132]);
        }];
        [self.lblTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.mas_equalTo([SFitUtil fit:36]);
        }];
        [self.lblTTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.bottom.mas_equalTo([SFitUtil fit:-23]);
        }];
    }
    return _imgvTop;
}
- (UILabel *)lblTime{
    if (_lblTime == nil){
        _lblTime = [SThemeUtil boldLabel:@"" color:STheme.H_D12 fontSize:35];
        _lblTime.font = [SFitUtil fitFont:35 name:@"DingTalk Sans"];
        _lblTime.textAlignment = NSTextAlignmentCenter;
        _lblTime.adjustsFontSizeToFitWidth = YES;
        _lblTime.minimumScaleFactor = 0.6;
    }
    return _lblTime;
}
- (UILabel *)lblTTitle{
    if (_lblTTitle == nil){
        _lblTTitle = [SThemeUtil boldLabel:KLanguage(@"count_down_txt") color:STheme.H_D12 fontSize:16];
        _lblTTitle.textAlignment = NSTextAlignmentCenter;
        _lblTTitle.adjustsFontSizeToFitWidth = YES;
        _lblTTitle.minimumScaleFactor = 0.6;
    }
    return _lblTTitle;
}
//- (UIButton *)btnStop{
//    if (_btnStop == nil){
//        _btnStop = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_btnStop setTitle:@"终止计时" forState:UIControlStateNormal];
//        [_btnStop setTitleColor:STheme.D2 forState:UIControlStateNormal];
//        _btnStop.titleLabel.font = [SFitUtil fitBoldFont:18];
//        [_btnStop addTarget:self action:@selector(stopClickAction:) forControlEvents:UIControlEventTouchUpInside];
//        _btnStop.backgroundColor = [SUIUtil colorOf:STheme.H_D12 alpha:0.1];
//        _btnStop.layer.cornerRadius = [SFitUtil fit:8];
//        _btnStop.clipsToBounds = YES;
//    }
//    return _btnStop;
//}
- (UIView *)vBox{
    if (_vBox == nil){
        _vBox = [[UIView alloc] init];
        _vBox.backgroundColor = STheme.D9;
        _vBox.clipsToBounds = YES;
        _vBox.layer.cornerRadius = [SFitUtil fit:8];
    }
    return _vBox;
}
- (UILabel *)lblTips{
    if (_lblTips == nil){
        _lblTips = [SThemeUtil boldLabel:@"" color:STheme.H_D1 fontSize:16];
        _lblTips.numberOfLines = 0;
        
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = [SFitUtil fit:8];
        NSAttributedString *attr = [[NSAttributedString alloc] initWithString:KLanguage(@"count_down_loading_hint_txt") attributes:@{NSParagraphStyleAttributeName: style}];
        _lblTips.attributedText = attr;
        _lblTips.textAlignment = NSTextAlignmentCenter;
    }
    return _lblTips;
}
- (UIImageView *)imgvIcon{
    if (_imgvIcon == nil){
        _imgvIcon = [[UIImageView alloc] init];
        _imgvIcon.image = [UIImage imageNamed:@"check_smapling_wait_card_icon"];
    }
    return _imgvIcon;
}
- (UIButton *)btnFinish{
    if (_btnFinish == nil){
        _btnFinish = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnFinish setTitle:KLanguage(@"finish_wait_txt") forState:UIControlStateNormal];
        [_btnFinish setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnFinish.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnFinish addTarget:self action:@selector(finishClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnFinish.backgroundColor = STheme.D12;
        _btnFinish.layer.cornerRadius = [SFitUtil fit:10];
        _btnFinish.clipsToBounds = YES;
    }
    return _btnFinish;
}

- (void)dealloc{
    [self stopTimer];
}

@end
